#!/bin/bash
#
##############################################################################
#                                                                            #
#                      Pi-Star MMDVMHost Remote Tool                         #
#                                                                            #
#     Version 1.0, Code, Design and Development by Andy Taylor (MW0MWZ).     #
#                                                                            #
#       Make it simple to use the MMDVM Remote from the CLI on Pi-Star.      #
#                                                                            #
##############################################################################
#
if [ "$(id -u)" != "0" ]; then
  echo -e "You need to be root to run this command...\n"
  exit 1
fi

# Setup some variables
cmd=/usr/local/bin/RemoteCommand
port=$(sed -n '/^\[Remote Control\]/,/^\[/p' /etc/mmdvmhost | grep "^Port" | awk -F "=" '{print $2}')

if [ -z "$1" ]; then
      echo "This is a wrapper script to make it more simple to use the MMDVMHost"
      echo "RemoteCommand tool by figuring out the port from the MMDVMHost config"
      echo "and helping to fill in some of the commands for you."
      echo ""
      echo "You can use this tool to sed the mode to:"
      echo "Idle, Lockout, D-Star, DMR, YSF, P25 or NXDN"
      echo ""
      echo "Simply run pistar-mmdvmremote followed by one of the above, so for"
      echo "example \"pistar-mmdvmremote lockout\""
      echo ""
      exit 0
fi


if [ ${1} ]; then
	# Make the input lower case
	inputcmd=$(echo ${1} | tr '[:upper:]' '[:lower:]')

	case ${inputcmd} in
		idle)
			${cmd} ${port} mode idle
		;;
		lockout)
			${cmd} ${port} mode lockout
		;;
		d-star)
			${cmd} ${port} mode d-star
		;;
		dmr)
			${cmd} ${port} mode dmr
		;;
		ysf)
			${cmd} ${port} mode ysf
		;;
		p25)
			${cmd} ${port} mode p25
		;;
		nxdn)
			${cmd} ${port} mode nxdn
		;;
		*)
			echo "Command not understood..."
		;;
	esac

	# Cleanup any left over logs that are dropped all over the place...
	rm -rf RemoteCommand-*.log > /dev/null 2>&1
fi

exit 0
